<template>
  <view class="content">
    <uni-forms
      :value="formData"
      ref="form"
      validate-trigger="bind"
      err-show-type="undertext"
    >
      <view class="max-title">基本信息</view>
      <view class="info">
        <view class="info-item">
          <view>姓名</view>
          <view>{{ personInfo.name || "" }}</view>
        </view>
        <view class="info-item">
          <view>证件类型</view>
          <view>{{ cardType || "" }}</view>
        </view>
        <view class="info-item">
          <view>证件号码</view>
          <view>{{ personInfo.cardId || "" }}</view>
        </view>
        <view class="info-item">
          <view>国籍</view>
          <view>{{ personInfo.nationality || "" }}</view>
        </view>
        <view class="info-item">
          <view>年龄</view>
          <view>{{ personInfo.age || "" }}</view>
        </view>
      </view>

      <view class="assessment">
        <view v-for="(item, index) in registerData" :key="index">
          <view class="max-title">{{ item.title }}</view>
          <view
            class="assessment-item"
            v-for="list in item.list"
            :key="list.label"
          >
            <view class="assessment-item-list">{{ list.label }}</view>
            <view
              class="assessment-item-btn"
              :class="{
                'assessment-item-btn-unsuccess': list.type == 2,
                'assessment-item-btn-success': list.type == 3,
              }"
              @tap="goRegister(list)"
            >
              <view v-if="list.type == 1">
                <image
                  src="https://szgd-dev.tgovcloud.com/public/images/edit.png"
                  mode="widthFix"
                ></image>
                去填写
              </view>
              <view v-if="list.type == 2">
                <image
                  src="https://szgd-dev.tgovcloud.com/public/images/unsuccessful.png"
                  mode="widthFix"
                ></image>
                未完成
              </view>
              <view v-if="list.type == 3">
                <image
                  src="https://szgd-dev.tgovcloud.com/public/images/successful.png"
                  mode="widthFix"
                ></image>
                已完成
              </view>
            </view>
          </view>
        </view>
      </view>
    </uni-forms>
    <view style="height: "></view>
  </view>
</template>

<script>
import { getData } from "../../api/index";
import { cardTypes } from "@/data";
import { translateDic } from "@/utils";
import { problemList } from "@/packageIsolate/pages/mentalHealth/questionList";

export default {
  data() {
    return {
      isChange: "0",
      name: "",
      identityType: "",
      idCode: "",
      nation: "",
      age: "",
      phone: "",
      isolationPoint: "",
      sex: "0",
      rzfh: "--",

      id: "",
      formData: {
        otherDiseases: "",
      },
      healthScreening: false, // 是否完成了健康排查
      registerData: [],
      healthList: [
        {
          title: "健康登记",
          list: [
            {
              label: "健康排查登记",
              src: "/pages/healthInspect/investigate",
              type: "1",
            },
          ],
        },
      ],
      psychologyList: [
        {
          title: "心理评估",
          list: [
            {
              label: "综合评估",
              src: "/pages/healthInspect/comprehensive",
              type: "1",
            },
            {
              label: "失眠严重指数",
              src: "/pages/healthInspect/insomnia",
              type: "2",
            },
            {
              label: "心理健康环境",
              src: "/pages/healthInspect/psychology",
              type: "2",
            },
            {
              label: "焦虑问卷",
              src: "/pages/healthInspect/anxious",
              type: "2",
            },
          ],
        },
      ],
      registerData: [],
      personInfo: {},
    };
  },
  filters: {
    sexPD: (value) => {
      if (value == 0) {
        return "女";
      } else if (value == 1) {
        return "男";
      } else {
        return "其他";
      }
    },
  },
  computed: {
    cardType() {
      let cardType = this.personInfo.cardType || "";
      cardType = translateDic(cardTypes, this.personInfo.cardType);
      return cardType;
    },
  },
  onShow(){
    this.getDataInfo();
  },
  onLoad(e) {
    this.addPerson = e.addPerson;
    this.isChange = e.isChange;
    this.personInfo = uni.getStorageSync("personInfo");
    //this.getGet()
    uni.setStorage({ key: "detailLits", data: "" });
    uni.setStorage({ key: "attitudeTowardsMedicalObservation", data: "" });
    uni.setStorage({ key: "assessmentRiskLevel", data: "" });
    uni.setStorage({ key: "degreeSuicideAndSelfInjury", data: "" });
    uni.setStorage({ key: "friendsName", data: "" });
    uni.setStorage({ key: "friendsPhone", data: "" });
    uni.setStorage({ key: "statusQuoinHeart", data: "" });
    uni.setStorage({ key: "assessmentDoctor", data: "" });
    uni.setStorage({ key: "assessmentTime", data: "" });
    uni.setStorage({ key: "healthStatusData", data: "" });
    uni.setStorage({ key: "results", data: "" });

    // 健康登记
    uni.setStorage({ key: "healthScreenData", data: "" });

    // 权限判断
    this.authJudgment();
  },
  methods: {
    // 权限控制
    authJudgment() {
      const personInfo = uni.getStorageSync("personalInfo");
      const authorities = personInfo.authorities;
      let healthList = []; // 健康登记
      let psychologyList = []; // 心理评估
      authorities.forEach((item) => {
        if (item.name == "健康登记") {
          healthList = this.healthList;
        } else if (item.name == "心理评估") {
          psychologyList = this.psychologyList;
        }
      });
      this.registerData = [...healthList, ...psychologyList];

      // 获取心理、健康的填写状态
      // this.getDataInfo();
    },
    goRegister(row) {
      const { src, label, type } = row;
      let query = "";
      if (this.addPerson) {
        query = `?addPerson=true&isChange=${this.isChange}`;
      } else {
        if (src.indexOf('?') > -1) {
          query = `&isChange=${this.isChange}`;
        } else {
          query = `?isChange=${this.isChange}`;
        }
        if (label == '健康排查登记' && type == 1) {
          query += '&isPreData=true'
        }
      }
      console.log('goRegister', src, query);
      uni.navigateTo({
        url: src+query,
      });
    },
    // 修改心理评估的状态
    changeState(res, item) {
      this.psychologyList[0].list.forEach((item) => {
        if (item.label == "焦虑问卷") {
          item.type = res[0] && res[0].dataGad7 == null ? 2 : 3;
        }
        if (item.label == "心理健康环境") {
          item.type = res[0] && res[0].dataPhq9 == null ? 2 : 3;
        }
        if (item.label == "失眠严重指数") {
          item.type = res[0] && res[0].dataIsi == null ? 2 : 3;
        }
      });
      if (res[0] && res[0].assessmentRiskLevel) {
        item.list[0].type = 3;
        item.list[0].src =
          "/pages/healthInspect/comprehensiveDetails?canEdit=1";
      } else {
        // 未填写
        item.list[0].type = 1;
        item.list[0].src = "/pages/healthInspect/comprehensive";
      }
      this.formatGrade(res);
    },
    // 获取心理、健康的填写状态
    getDataInfo() {
      let { personId, cardId, name } = uni.getStorageSync("personInfo");
      let params = {
        personId,
      };
      const that = this;
      this.registerData.map((item) => {
        if (item.title == "健康登记") {
          // 健康排查 判断健康排查是否已完成
          getData("healthCheck", "get", params).then((res) => {
            // 已填写
            if (res.isolateId != null) {
              item.list[0].type = 3;
              item.list[0].src = `/pages/healthInspect/investigateDetails?canEdit=1&isChange=${that.isChange}`;
            } else {
              // 未填写
              item.list[0].type = 1;
              item.list[0].src = `/pages/healthInspect/investigate?isChange=${that.isChange}`;
            }
          });
        } else if (item.title == "心理评估") {
          // 心理评估 判断是否完成
          if (this.addPerson) {
            let param = {
              name,
              identity: cardId,
            };
            getData("latest", "get", param).then((res) => {
              this.changeState([res], item);
            });
          } else {
            getData("comprehensive", "get", params)
              .then((res) => {
                // 已填写
                this.changeState(res, item);
              })
              .catch((e) => {
                console.log(e);
              });
          }
        }
      });
    },
    // get 刚进页面的Get请求
    // getGet(){
    // let  personId = this.id
    // let parms = {personId}
    // getData('healthCheck','get',parms).then( (res)=>{
    //      console.log( 'res',res);
    //      this.name  = res.name
    //      this.identityType =  res.identityType
    //      this.nation = res.nation
    //      this.age = res.age
    //      this.phone= res.phone
    //      this. isolationPoint= res.isolationPoint
    //   } ).catch((e)=>{
    //     console.log( e);
    //   })
    // },
    // 发送数据
    putData() {},

    // 处理“失眠严重指数”、“心理健康环境”、“焦虑问卷”数据回显，存到storage
    formatGrade(res){
      try {
        // 失眠
        var dataIsi = JSON.parse(res[0].dataIsi),
        dataGad7 = JSON.parse(res[0].dataGad7),
        dataPhq9 = JSON.parse(res[0].dataPhq9);
        if(dataIsi !=null && dataIsi !=''){
          this.formatQl(dataIsi, 'dataIsi');
        }
        if(dataGad7 !=null && dataGad7 !=''){
          this.formatQl(dataGad7, 'dataGad7');
        }
        if(dataPhq9 !=null && dataPhq9 !=''){
          this.formatQl(dataPhq9, 'dataPhq9');
        }

        // 保存此次查询结果，供修改时填充页面上没有的字段。解决3张表修改时综合评估数据丢失问题。
        uni.setStorage({
          key: 'healthInspectRes',
          data: res[0]? res[0] : {}
        })

      } catch (error) {
        console.log('error===', error)
      }
    },

    formatQl(list, propertyName){
      let dataArr=JSON.parse(JSON.stringify(problemList[propertyName]));
      dataArr.forEach(item => {
        for(let k=0;k<list.length;k++){
          let obj=list[k];
          if(item.order===obj.name){
            try {
              item.num=Number(obj.value);
              item.answer=item.opts[obj.value].opt;
            } catch (error) {}
            break;
          }
        }
      })
      uni.setStorage({
        key: propertyName,
        data: dataArr,
      })
    },

  },
};
</script>

<style lang='scss' scoped>
.content {
  display: flex;
  flex-direction: column;
  .uni-forms-item__label {
    width: 300rpx !important;
  }
}
.title {
  font-size: 40rpx;
  color: #000000;
  font-weight: bold;
}
.max-title {
  font-size: 40rpx;
  color: #000000;
  font-weight: 600;
  border-top: 30rpx solid #f9f9f9;
  border-bottom: 1px solid #f9f9f9;
  padding: 30rpx 40rpx;
  box-sizing: border-box;
}
.assessment {
  &-item {
    display: flex;
    padding-top: 40rpx;
    padding-bottom: 40rpx;
    padding-left: 40rpx;
    padding-right: 40rpx;
    font-size: 30rpx;
    box-sizing: border-box;
    &-list {
      flex: 1;
      color: #999;
    }
    &-btn {
      flex: 1;
      text-align: right;
      color: #2c9af1;
      &-success {
        color: #13c563;
      }
      &-unsuccess {
        color: #f79672;
      }
      image {
        width: 32rpx;
        display: inline-block;
        margin-right: 20rpx;
      }
    }
  }
}
.info {
  font-size: 30rpx;
  padding-left: 40rpx;
  padding-right: 40rpx;
  &-item {
    display: flex;
    justify-content: space-between;
    // padding-top: 40rpx;
    // padding-bottom: 40rpx;
    padding-top: 18rpx;
    padding-bottom: 18rpx;
    border-bottom: 1px solid #f9f9f9;
    font-weight: 500;
    &:last-child {
      border-bottom: 0;
    }
  }
}
.info view view:first-child {
  color: #999999;
  border-bottom: 0;
}
.info view view:last-child {
  color: #666666;
  font-size: 34rpx;
  border-bottom: 0;
}
.lettle-title {
  font-size: 40rpx;
  font-weight: 600;
  line-height: 98rpx;
  height: 98rpx;
}

.input-style {
  font-size: 24rpx;
  margin-top: 20px;
  border-bottom: 1rpx solid #e4e4e4;
}
.flex-box {
  height: 100rpx;
  line-height: 100rpx;
  font-size: 26rpx;
  display: flex;
  justify-content: space-between;
  color: #333333;
  text {
    font-size: 26rpx;
  }
}
.btn {
  width: 686rpx;
  height: 94rpx;
  background: #2c9af1;
  border-radius: 16rpx;
}

// .item-jbxx {
//   /* display: flex;
//   justify-content: space-between;
//   height: 130rpx; */
// }
.small-title {
  font-size: 30rpx;
  font-family: PingFang SC;
  font-weight: bold;
  color: #000000;
  margin: 40rpx 0 0 0;
}
</style>
